DROP TRIGGER UPDATE_T2_ID_TRI;
DROP TRIGGER UPDATE_T1_ID_TRI;
DROP SEQUENCE T2_PK_SEQ;
DROP SEQUENCE T1_PK_SEQ;
DROP TABLE T2;
DROP TABLE T1;

--创建表
CREATE TABLE T1(
C1 INT,
C2 VARCHAR(10) NOT NULL,
C3 VARCHAR(10) NOT NULL,
C4 VARCHAR(10) NOT NULL,
C5 VARCHAR(10) NOT NULL,
CONSTRAINT PK_T1C1 PRIMARY KEY (C1)
);

CREATE TABLE T2(
C6 INT PRIMARY KEY,
C7 VARCHAR(10) NOT NULL,
C8 VARCHAR(10) NOT NULL,
C9 VARCHAR(10) NOT NULL,
C10 VARCHAR(10) NOT NULL,
C11 CONSTRAINT FK_T2C11 REFERENCES T1(C1)
);

--创建序列
CREATE SEQUENCE T1_PK_SEQ
INCREMENT BY 1
START WITH 1
MAXVALUE 65536
CYCLE
NOCACHE;

CREATE SEQUENCE T2_PK_SEQ
INCREMENT BY 1
START WITH 1
MAXVALUE 65536
CYCLE
NOCACHE;

--创建触发器
CREATE TRIGGER UPDATE_T1_ID_TRI BEFORE
INSERT ON T1 FOR EACH ROW WHEN(NEW.C1 IS NULL)
BEGIN
SELECT T1_PK_SEQ.NEXTVAL INTO:NEW.C1 FROM DUAL;
END;

CREATE TRIGGER UPDATE_T2_ID_TRI BEFORE
INSERT ON T2 FOR EACH ROW WHEN(NEW.C6 IS NULL)
BEGIN
SELECT T2_PK_SEQ.NEXTVAL INTO:NEW.C6 FROM DUAL;
END;
